package br.com.ss.portal.controller.relatorio;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import br.com.ss.portal.component.ConnectionFactory;
import br.com.ss.portal.model.entity.Eleitor;
import br.com.ss.portal.model.entity.Responsavel;

@Controller
@ManagedBean(name = "relatorioController")
// @Scope("session")
@ApplicationScoped
public class RelatorioController {

	private Responsavel responsavel = new Responsavel();
	private Responsavel selected;
	private List<Responsavel> responsaveis = new ArrayList<Responsavel>();
	private List<Eleitor> eleitores = new ArrayList<Eleitor>();

	@Autowired
	private IRelatorioService services;

	@PostConstruct
	public void init() {
		System.out.println("entrou no init");
		this.responsaveis = services.listResponsaveis();
		this.eleitores = services.listEleitores();
		this.responsavel = new Responsavel();
	}

	public void setSelected(Responsavel selected) {
		this.selected = selected;
	}

	public Responsavel getSelected() {
		return selected;
	}

	public Responsavel getResponsavel() {
		return responsavel;
	}

	public void setResponsavel(Responsavel responsavel) {
		this.responsavel = responsavel;
	}

	public List<Responsavel> getResponsaveis() {
		return responsaveis;
	}

	public void setResponsaveis(List<Responsavel> responsaveis) {
		this.responsaveis = responsaveis;
	}

	public List<Eleitor> getEleitores() {
		return eleitores;
	}

	public void setEleitores(List<Eleitor> eleitores) {
		this.eleitores = eleitores;
	}

	@SuppressWarnings("deprecation")
	public void imprimir() throws IOException, JRException {
		ByteArrayOutputStream byteOutPutStream = new ByteArrayOutputStream();

		try {
			FacesContext facesContext = FacesContext.getCurrentInstance();

			this.eleitores.containsAll(services.listEleitores());

			responsavel.setEleitores(this.eleitores);

			HttpServletResponse response = (HttpServletResponse) facesContext
					.getExternalContext().getResponse();

			ServletContext servletContext = (ServletContext) facesContext
					.getExternalContext().getContext();

			String pathJasper = servletContext
					.getRealPath("/pages/relatorio/relatorioResponsavelEleitores.jasper");

			JasperPrint preencher = JasperFillManager.fillReport(pathJasper,
					null, new JRBeanCollectionDataSource(this.eleitores));

			System.out.println("Writing to InPut stream...");
			JasperExportManager.exportReportToPdfStream(preencher,
					byteOutPutStream);

			System.out.println("Size of OutPut: " + byteOutPutStream.size());
			response.setContentLength(byteOutPutStream.size());
			response.setContentType("application/pdf");
			// response.setHeader("Content-Disposition","attachment; filename=wpinvenotry-"
			// + new Date() + ".pdf");
			// response.setHeader("Cache-Control", "no-cache");

			System.out.println("Writing to OutPut stream...");
			ServletOutputStream servletOutPutStream = response
					.getOutputStream();
			servletOutPutStream.write(byteOutPutStream.toByteArray(), 0,
					byteOutPutStream.size());

			System.out.println("Closing stream...");
			servletOutPutStream.flush();
			servletOutPutStream.close();

			FacesContext.getCurrentInstance().responseComplete();

		} catch (JRException jrex) {
			jrex.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	public void imprimirGrafico() throws IOException, JRException {
		ByteArrayOutputStream byteOutPutStream = new ByteArrayOutputStream();

		try {
			FacesContext facesContext = FacesContext.getCurrentInstance();

			Connection connection = ConnectionFactory.getConnection();

			HttpServletResponse response = (HttpServletResponse) facesContext
					.getExternalContext().getResponse();

			ServletContext servletContext = (ServletContext) facesContext
					.getExternalContext().getContext();

//			String pathJasper = servletContext
//					.getRealPath("/page/relatorio/report2.jasper");
			
			String pathJasper = "c:\\report2.jasper";

			JasperPrint preencher = JasperFillManager.fillReport(pathJasper,
					null, connection);

			System.out.println("Writing to InPut stream...");
			JasperExportManager.exportReportToPdfStream(preencher,
					byteOutPutStream);

			System.out.println("Size of OutPut: " + byteOutPutStream.size());
			response.setContentLength(byteOutPutStream.size());
			response.setContentType("application/pdf");
			// response.setHeader("Content-Disposition","attachment; filename=wpinvenotry-"
			// + new Date() + ".pdf");
			// response.setHeader("Cache-Control", "no-cache");

			System.out.println("Writing to OutPut stream...");
			ServletOutputStream servletOutPutStream = response
					.getOutputStream();
			servletOutPutStream.write(byteOutPutStream.toByteArray(), 0,
					byteOutPutStream.size());

			System.out.println("Closing stream...");
			servletOutPutStream.flush();
			servletOutPutStream.close();

			FacesContext.getCurrentInstance().responseComplete();

		} catch (JRException jrex) {
			jrex.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}